// 摇杆控制
var isDragging = false;
var lastX, lastY;

//PC端
$(document).mousedown(function (e) {
    if ($(e.target).closest('.pause-box').length
        || $(e.target).closest('#mask').length
        || $(e.target).closest('#popup').length
        || $(e.target).closest('#skill-popup').length) {
        return; // 如果在禁止点击元素上点击，则不执行后续操作
    }
    isDragging = true;
    lastX = e.pageX;
    lastY = e.pageY;
    //将遥杆定位到该位置
    $(".action-box").css({
        "left": lastX - 50 + "px",
        "top": lastY - 50 + "px"
    });
    $(".action").css({
        "left": lastX - 10 + "px",
        "top": lastY - 10 + "px"
    });
});

$(document).mousemove(function (e) {
    baseW = $("body").width()
    if (isDragging) {
        var currentX = e.pageX;
        var currentY = e.pageY;
        var deltaX = currentX - lastX;
        var deltaY = currentY - lastY;
        var d = Math.sqrt(Math.pow(deltaX, 2) + Math.pow(deltaY, 2));
        angle = Math.atan2(deltaY, deltaX) * (180 / Math.PI);
        if (d > 50) {
            var angleInRadians = angle * Math.PI / 180;

            // 计算水平和垂直方向上的速度
            var r = d > 50 ? 50 : d
            var velocityX = Math.cos(angleInRadians) * r + lastX;
            var velocityY = Math.sin(angleInRadians) * r + lastY;
            $(".action").css({
                "left": velocityX - 10 + "px",
                "top": velocityY - 10 + "px"
            });
        } else {
            $(".action").css({
                "left": currentX - 10 + "px",
                "top": currentY - 10 + "px"
            });
        }


    }
});

$(document).mouseup(function () {
    isDragging = false;
    $(".action").css({
        "left": lastX - 10 + "px",
        "top": lastY - 10 + "px"
    });
});


//移动端
$(document).on('touchstart', function (e) {
    isDragging = true;
    lastX = e.originalEvent.touches[0].pageX;
    lastY = e.originalEvent.touches[0].pageY;
    //将遥杆定位到该位置
    $(".action-box").css({
        "left": lastX - 50 + "px",
        "top": lastY - 50 + "px"
    });
    $(".action").css({
        "left": lastX - 10 + "px",
        "top": lastY - 10 + "px"
    });
});

$(document).on('touchmove', function (e) {
    baseW = $("body").width()
    if (isDragging) {
        var currentX = e.originalEvent.touches[0].pageX;
        var currentY = e.originalEvent.touches[0].pageY;
        var deltaX = currentX - lastX;
        var deltaY = currentY - lastY;
        var d = Math.sqrt(Math.pow(deltaX, 2) + Math.pow(deltaY, 2));
        angle = Math.atan2(deltaY, deltaX) * (180 / Math.PI);
        if (d > 50) {
            var angleInRadians = angle * Math.PI / 180;

            // 计算水平和垂直方向上的速度
            var r = d > 50 ? 50 : d
            var velocityX = Math.cos(angleInRadians) * r + lastX;
            var velocityY = Math.sin(angleInRadians) * r + lastY;
            $(".action").css({
                "left": velocityX - 10 + "px",
                "top": velocityY - 10 + "px"
            });
        } else {
            $(".action").css({
                "left": currentX - 10 + "px",
                "top": currentY - 10 + "px"
            });
        }
    }
});

$(document).on('touchend', function () {
    isDragging = false;
    $(".action").css({
        "left": lastX - 10 + "px",
        "top": lastY - 10 + "px"
    });
});